<template>
  <div class="public-explain-action app-container">
    <div class="public-explain-top">
      <div v-if="$route.query.action === 'add'">新增</div>
      <div v-else-if="$route.query.action === 'edit'">编辑</div>
      <div v-else-if="$route.query.action === 'detail'">详情</div>
      <div class="action-btn-box" v-if="$route.query.action !== 'detail'">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
        >确定</el-button>
        <el-button
          plain
          size="mini"
          @click="goBack"
        >取消</el-button>
      </div>
      <div class="action-btn-box" v-else>
        <el-button
          plain
          size="mini"
          @click="goBack"
        >返回</el-button>
      </div>
    </div>
    <div class="public-explain-content">
      <div class="public-explain-left">
        <el-form :model="queryParams" :rules="rules" label-position="top" ref="queryForm">
            <el-form-item label="关键词" prop="searchKey">
                <el-input
                  v-model="queryParams.searchKey"
                  placeholder="请输入关键词"
                  :disabled="$route.query.action === 'detail'"
                  clearable
                  size="small"
                  style="width: 500px"
                >
                </el-input>
            </el-form-item>
            <el-form-item label="回复" prop="answer">
                <el-input
                  v-model="queryParams.answer"
                  placeholder="请输入回复"
                  :disabled="$route.query.action === 'detail'"
                  clearable
                  size="small"
                  style="width: 500px"
                >
                </el-input>
            </el-form-item>
        </el-form>
      </div>
    </div>

  </div>
</template>

<script>
import { updateFn, add, list } from "@/api/form/customAnswer";
import { dictFn } from "@/api/common";
import { getToken } from "@/utils/auth";
import {
  jingQuListApi, // 获取所有景区列表
} from "@/api/YApi/scenicSpot";

export default {
  name: "Logininfor",
  data() {
    return {
      rules: {
          searchKey: [
            { required: true, message: '请输入关键词', trigger: 'change' }
          ],
          answer: [
            { required: true, message: '请输入回复', trigger: 'change' }
          ],
          questionType: [
            { required: true, message: '请选择问题类型', trigger: 'change' }
          ],
      },
      // 遮罩层
      loading: true,
      uploadUrl: window.serverAPI.url + "/common/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 导出遮罩层
      exportLoading: false,
      // 查询参数
      queryParams: {
        answer: undefined,
        searchKey: undefined,
      }
    };
  },
  created() {
    this.$route.query.id && this.getList();
  },
  methods: {
    getList() {
      let {pageNum, pageSize, id} = this.$route.query
      list({
        pageNum, pageSize
      }).then(response => {
        let arr = response.data.rows;
        arr.filter(item => item?.id === id)
        let obj = arr?.[0] || {};
        this.queryParams = obj
      });
    },
    handleAdd() {
      this.$refs.queryForm.validate((tag, e) => {
        if (tag) {
          let fun = this.$route.query.id ? updateFn : add
          fun({
            ...this.queryParams,
          }).then(res => {
            if (res?.code === 200) {
              this.msgSuccess("保存成功");
              this.goBack();
            } else {
              this.$message.error(res?.msg);
            }
          })
        }
      });
    },
    goBack() {
      this.$router.go(-1);
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.public-explain-action {
  background-color: #ffffff;
  .public-explain-top {
    border-bottom: 1px solid #F2F3F5;
    display: flex;
    justify-content: space-between;
    padding: 12px;
    align-items: center;
  }
  .fileRow {
    display: flex;
    >* {
      margin-right: 20px;
    }
  }
  .public-explain-content {
    display: flex;
    .detail-title {
      position: relative;
      padding-left: 12px;
      padding-block: 20px;
      &::after {
        content: '';
        width: 4px;
        height: 16px;
        background: #3370FF;
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        margin: auto 0;

      }
    }
  }
  .actionIcon{
    display: flex;
    color: #3370FF;
    padding-top: 50px;
    >div{
      cursor: pointer;
      white-space: nowrap;
    }
    .delete {
      color: #F54A45;
      margin-left: 20px;
    }
  }
}
</style>
